<?php

namespace app\admin\model\cms;

use think\Model;
use traits\model\SoftDelete;

/**
 * 广场文章
 * Class PlazaPostModel
 * @package app\admin\model\cms
 */
class PlazaPostModel extends Model
{


    // 表名
    protected $name = 'plaza_post';

    //软删除
    use SoftDelete;

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    //自动写入时间戳的格式
    protected $dateFormat = 'Y-m-d H:i:s';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'category_name'
    ];

    public function getCategoryNameAttr($value, $data)
    {
        $value = $value ? $value : $data['plaza_category_id'];
        $name  = PlazaCategorymodel::where('id', '=', $value)->value('platename');
        return !empty($name) ? $name:'';
    }

    public function category()
    {
        return $this->belongsTo('app\admin\model\cms\PlazaCategorymodel', 'plaza_category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function user(){
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->field('id,username,nickname,avatar,bio,email')->setEagerlyType(1);
    }

    public function commentList(){
        return $this->hasMany('app\admin\model\cms\PlazaCommentModel', 'id', 'plaza_post_id');
    }
}
